const {
    updateBikeCollectStatusServe,
    getCollectByUserIdServe,
    deleteCollectByUserIdServe
} = require('../serve/appletBikeCollect.serve')

class AppletBikeCollectController {
    async updateBikeCollectStatus(ctx) {
        try {
            const {bikeId} = ctx.params;
            if(!bikeId){
                throw ({message: '更新失败'})
            }
            const {id: userId} = ctx.state.appletUser;
            let msg = ''
            const collectBike = await getCollectByUserIdServe({bikeId, userId})
            if (collectBike) {
                await deleteCollectByUserIdServe(bikeId, userId)
                msg = '取消收藏'
            } else {
                await updateBikeCollectStatusServe(bikeId, userId)
                msg = '收藏成功'
            }
            ctx.body = {
                status: 0,
                msg
            }
        } catch (e) {
            ctx.body = {
                status: 1,
                msg: e.message
            }
        }
    }
}

module.exports = new AppletBikeCollectController()
